import connector.datebasedriver
from connector.datebasedriver import datebasedriver


# 根据书籍分类名查询分类号
def get_cate_id_by_cate_name(driver: datebasedriver, student_Sname: str):
    return driver.select_one(sql="select Sno from student_information where Sname='{}'".format(student_Sname), log=True)


def get_cate_by_cate_name(driver: datebasedriver, student_Sname: str):
    return driver.select_one(sql="select * from student_information where Sname like '%{}%'".format(student_Sname), log=True)

# 获取所有书籍分类
def get_cates(driver:datebasedriver)->tuple:
    return driver.select_all("select * from student_information", log=True)

# 根据id获取类别
def get_cate_by_id(driver: datebasedriver, student_Sno: int)-> tuple:
    return driver.select_one("select * from student_information where Sno = {}".format(student_Sno), log=True)

def update_cate(driver: datebasedriver, student_Sno: int, new_Sname_name: str) -> int:
    return driver.update("update student_information set Sname='{}' where Sno={}".format(new_Sname_name, student_Sno))

def delete_cate(driver: datebasedriver, student_Sno: int) -> int:
    return driver.delete("delete from student_information where Sno={}".format(student_Sno))

def insert_cate(driver: datebasedriver, Sno: int,Sname: str) -> int:
    if Sno is not None:
        return driver.insert("insert into student_information(Sno,Sname) values({},'{}')".format(Sno,Sname))
    else:
        return driver.insert("insert into student_information(Sname) values('{}')".format(Sname))